﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Prod;
using System.Collections;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageProductionSchduleList : BaseForm.frmBaseMdiChildForm
    {
        #region vars
        private DataSet dsBindData = new DataSet();
        #endregion

        #region ctor
        public frmManageProductionSchduleList()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbProductPlanOrderStatus, "PSTATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colProdPlanOrderStatus, "PSTATUS", false);
        }

        private string GetCondition()
        {
            string sCondition = "";
            //if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            //{
            //    sCondition += "and sppai.PRODUCT_SPEC_OUT like '%" + txtContractModel.Text + "%' ";
            //}
            //if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            //{
            //    sCondition += "and mp.PRODUCT_CODE like '%" + txtProductCode.Text + "%' ";
            //}
            if (string.IsNullOrEmpty(cmbProductPlanOrderStatus.SelectedValue.ToString()) == false)
            {
                sCondition += "and ppo.STATUS='" + cmbProductPlanOrderStatus.SelectedValue.ToString() + "' ";
            }
            else
            {
                sCondition += "and ppo.STATUS not in ('PSTATUS04','PSTATUS05','PSTATUS07') ";
            }
            if (dtpDeliveryDate.EditValue != null)
            {
                sCondition += "and ppo.DELIVERY_DATE>='"+((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd")+" 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue != null)
            {
                sCondition += "and ppo.DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            //if (string.IsNullOrEmpty(txtProductPlanApplication.Text) == false)
            //{
            //    sCondition += "and sppa.APPLICATION_CODE like '%" + txtProductPlanApplication.Text + "%' ";
            //}
            //if (string.IsNullOrEmpty(txtProductPlanOrder.Text) == false)
            //{
            //    sCondition += "and ppo.PLAN_ORDER_CODE like '%" + txtProductPlanOrder.Text + "%' ";
            //}
            return sCondition;
        }

        private DataTable GetGridDataProdPlanOrder()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = " PPO.PLAN_ORDER_CODE DESC";
            DataTable dt = ProdPlanOrderBs.GetProdPlanOrderByCondition(condition, orderBy, rowCount);
            return dt;
        }
        private DataTable GetGridDataProdWorkingOrder(DataTable dtProdPlanOrder)
        {
            if (dtProdPlanOrder==null)
            {
                return null;
            }
            if (dtProdPlanOrder.Rows.Count == 0)
            {
                return null;
            }
            string sCondition = " and ppoi.PLAN_ORDER_ID in (";
            foreach (DataRow dr in dtProdPlanOrder.Rows)
            {
                sCondition += "'"+dr["PLAN_ORDER_ID"].ToString()+"',";
            }
            sCondition = sCondition.Substring(0, sCondition.Length - 1);
            sCondition += ") ";
            DataTable dt=ProdPlanOrderItemBs.GetDtAllByCondition(sCondition, "", "");
            return dt;
        }
        private DataTable GetGridDataProdReportInfos(ProdPlanOrderItemEntity entity)
        {
            DataTable dt = new DataTable();
            if (entity == null)
            {
                dt = ProdWorkingReportBs.GetDtWorkingReportByItemID("");
            }
            else
            {
                dt = ProdWorkingReportBs.GetDtWorkingReportByItemID(entity.PlanOrderItemId);
            }
            return dt;
        }

        private void BindGridData()
        {
            dsBindData = new DataSet();
            DataTable dtProdPlanOrder = GetGridDataProdPlanOrder();
            if (dtProdPlanOrder == null)
            {
                return;
            }
            dtProdPlanOrder.TableName = "ProdPlanOrder";
            DataTable dtProdWorkingOrder = GetGridDataProdWorkingOrder(dtProdPlanOrder);
            if (dtProdWorkingOrder == null)
            {
                return;
            }
            dtProdWorkingOrder.TableName = "ProdWorkingOrder";
            DataTable dtProdReportInfos = GetGridDataProdReportInfos(null);
            dtProdReportInfos.TableName = "ProdReportInfos";

            dsBindData.Tables.Add(dtProdPlanOrder.Copy());
            dsBindData.Tables.Add(dtProdWorkingOrder.Copy());
            dsBindData.Tables.Add(dtProdReportInfos.Copy());
            DataColumn KeyColumn = dsBindData.Tables[0].Columns["PLAN_ORDER_ID"];         //主键
            DataColumn ForeignColumn = dsBindData.Tables[1].Columns["PLAN_ORDER_ID"];    //外键
            dsBindData.Relations.Add("工令单明细", KeyColumn, ForeignColumn);     //从表的层次名
            DataColumn KeyColumn2 = dsBindData.Tables[1].Columns["PLAN_ORDER_ITEM_ID"];         //主键
            DataColumn ForeignColumn2 = dsBindData.Tables[2].Columns["PLAN_ORDER_ITEM_ID"];    //外键
            dsBindData.Relations.Add("报工明细", KeyColumn2, ForeignColumn2);     //从表的层次名

            gvMain.DataSource = dsBindData.Tables[0];
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            BindCombobox();
            base.OnShown(e);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGridData();
        }

        private void gridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView aView = (DevExpress.XtraGrid.Views.Grid.GridView)gridView1.GetDetailView(e.RowHandle, 0);
            if (aView != null)
            {
                DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit colProdWorkingOrderStatus = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
                aView.Columns["STATUS"].ColumnEdit = colProdWorkingOrderStatus;
                UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colProdWorkingOrderStatus, "POI", false);
                aView.BestFitColumns();
                aView.Name = "工令单信息";
                aView.Columns["ORDER_ITEM_CODE"].Caption = "工令单";
                aView.Columns["ORDER_ITEM_CODE"].VisibleIndex = 0;
                aView.Columns["PRODUCT_NAME"].Caption = "物料名称";
                aView.Columns["ORDER_ITEM_CODE"].VisibleIndex = 1;
                aView.Columns["DELIVERY_DATE"].Caption = "计划交期";
                aView.Columns["ORDER_ITEM_CODE"].VisibleIndex = 2;
                aView.Columns["QTY"].Caption = "计划数量";
                aView.Columns["ORDER_ITEM_CODE"].VisibleIndex = 3;
                aView.Columns["STATUS"].Caption = "状态信息";
                aView.Columns["ORDER_ITEM_CODE"].VisibleIndex = 4;
                for (int i = 0; i < aView.Columns.Count; i++)
                {
                    if (!(aView.Columns[i].FieldName.Equals("ORDER_ITEM_CODE") ||
                        aView.Columns[i].FieldName.Equals("PRODUCT_NAME") ||
                        aView.Columns[i].FieldName.Equals("DELIVERY_DATE") ||
                        aView.Columns[i].FieldName.Equals("QTY") ||
                        aView.Columns[i].FieldName.Equals("STATUS")))
                    {
                        aView.Columns[i].VisibleIndex = -1;
                    }
                }
                aView.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(aView_RowClick);
                aView.MasterRowExpanded += new DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventHandler(aView_MasterRowExpanded);
            }
        }

        private ArrayList arrayList = new ArrayList();
        private void aView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (arrayList.IndexOf(e.RowHandle) != -1)
            {
                return;
            }
            DevExpress.XtraGrid.Views.Grid.GridView aView=(DevExpress.XtraGrid.Views.Grid.GridView)sender;
            DataRow dr = aView.GetDataRow(e.RowHandle);
            ProdPlanOrderItemEntity mPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            DataTable dtProdReportInfos = GetGridDataProdReportInfos(mPlanOrderItem);
            foreach (DataRow drProdReportInfos in dtProdReportInfos.Rows)
            {
                dsBindData.Tables[2].ImportRow(drProdReportInfos);
            }
            arrayList.Add(e.RowHandle);
        }

        private void aView_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridSender = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
            DevExpress.XtraGrid.Views.Grid.GridView aView = (DevExpress.XtraGrid.Views.Grid.GridView)gridSender.GetDetailView(e.RowHandle, 0);
            if (aView != null)
            {
                aView.BestFitColumns();
                aView.Name = "报工信息";
                aView.Columns["TECHNIC_NAME"].Caption = "工序名称";
                aView.Columns["REPORT_DATE"].Caption = "报工时间";
                aView.Columns["REPORT_QTY"].Caption = "报工数量";
                aView.Columns["NAME_CN"].Caption = "报工人";
                aView.Columns["TECHNIC_TYPE"].Caption = "工序类型";
                for (int i = 0; i < aView.Columns.Count; i++)
                {
                    if (!(aView.Columns[i].FieldName.Equals("TECHNIC_NAME") ||
                        aView.Columns[i].FieldName.Equals("RREPORT_DATE") ||
                        aView.Columns[i].FieldName.Equals("REPORT_QTY") ||
                        aView.Columns[i].FieldName.Equals("NAME_CN")||
                        aView.Columns[i].FieldName.Equals("TECHNIC_TYPE")))
                    {
                        aView.Columns[i].VisibleIndex = -1;
                    }
                }
            }
        }

        #endregion
    }
}
